package com.huabo.monitor.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hbfk.entity.DealUserToken;
import com.hbfk.entity.TblStaffUtil;
import com.hbfk.util.FtpUtil;
import com.hbfk.util.JsonBean;
import com.hbfk.util.ResponseFormat;
import com.huabo.monitor.entity.TblAttachment;
import com.huabo.monitor.entity.TblRiskAttWord;
import com.huabo.monitor.mapper.YhrPageMapper;
import com.huabo.monitor.service.ITblAttachmentService;
import com.huabo.monitor.service.ITblRiskAttWordService;
import com.huabo.monitor.util.ConstClass;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * @author：yhr
 * @date:2022-09-13 10:39
 * @description:
 */
@Controller
@RequestMapping("/filec")
@Api(value = "文件操作", tags = {"文件操作接口"})
@Slf4j
public class FileController {

    @Autowired
    ITblRiskAttWordService iTblRiskAttWordService;
    @Autowired
    ITblAttachmentService iTblAttachmentService;

    @Resource
    YhrPageMapper  yhrPageMapper;

    @RequestMapping(value = "/download", method = {RequestMethod.GET})
    @ApiOperation("附件下载接口")
    public void fileDownLoad(HttpServletRequest request, HttpServletResponse response,

                             @ApiParam(name="attId",value="附件主键ID",required=true) @RequestParam("attId") String attId) throws Exception {
//        TblStaffUtil loginStaff = DealUserToken.parseUserToken(token);
//        if(loginStaff == null) {
//            System.out.println("用户已失效！");
//        }

        TblAttachment tblAttachmentEntity = iTblAttachmentService.getById(attId);
        if (tblAttachmentEntity == null) {

            throw new Exception("附件不存在或已经删除");
        }
        com.hbfk.entity.TblAttachment tblAttachment = new com.hbfk.entity.TblAttachment();
        tblAttachment.setAttname(tblAttachmentEntity.getAttname());
        tblAttachment.setAttpath(tblAttachmentEntity.getAttpath());
        tblAttachment.setFileName(tblAttachmentEntity.getAttname());
        tblAttachment.setAttsize(tblAttachmentEntity.getAttsize().doubleValue());
        FtpUtil.downUploadFileNew(tblAttachment, response);
        System.out.println("下载成功！");
    }


//    @RequestMapping(value = "/delete", produces = "application/json; charset=utf-8", method = {RequestMethod.GET})
//    @ApiOperation("附件删除接口，不删除中间表关系")
//    public JsonBean fileRemove(HttpServletRequest request, HttpServletResponse response,
//                               @ApiParam(name="token",value="登录用户token",required=true) @RequestHeader("token")String token,
//                               @ApiParam(name="attId",value="附件主键ID",required=true) @RequestParam("attId") String attId) throws Exception {
//        TblStaffUtil loginStaff = DealUserToken.parseUserToken(token);
//        if(loginStaff == null) {
//            return ResponseFormat.retParam(0,20006,null);
//        }
//
//        TblAttachment tblAttachmentEntity = iTblAttachmentService.getById(attId);
//        if (tblAttachmentEntity == null) {
//            return ResponseFormat.retParam(0,203,null);
//        }
//        FtpUtil.removeFile(tblAttachmentEntity.getAttpath());
//        //this.tblAttachmentService.delete(attId);
//        return ResponseFormat.retParam(1,200,null);
//    }



    @GetMapping(value = "/getAttMsg")
    @ApiOperation("附件单查一条接口")
    @ResponseBody
    public JsonBean getAttMsg(

                             @ApiParam(name="attId",value="附件主键ID",required=true) @RequestParam("attId") String attId,
                             @ApiParam(name = "token", value = "登录用户token", required = true) @RequestHeader("token") String token
    ) throws Exception {
        if (!ConstClass.checkToken(token)) {
            return ConstClass.tokenFailure();
        }

//        TblStaffUtil loginStaff = DealUserToken.parseUserToken(token);
//        if(loginStaff == null) {
//            System.out.println("用户已失效！");
//        }

        TblAttachment tblAttachmentEntity = iTblAttachmentService.getById(attId);

        Map<String, Object> mv = new HashMap<>();

        mv.put("att", tblAttachmentEntity);
        return new JsonBean(200, "success", mv);
    }



}
